﻿using JNPF.Basics.Models.DataInterface.Entities;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic.DataInterface
{
    /// <summary>
    /// 数据接口
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.11.07 
    /// </summary>
    public class DataInterfaceService : DataFactory, DataInterfaceIService
    {
        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        public async Task<PageResult<DataInterfaceEntity>> GetList(PageInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<DataInterfaceEntity>();
                var queryParam = input.queryJson.ToObject();
                if (!string.IsNullOrEmpty(queryParam["categoryId"].ToString()))
                {
                    queryWhere = queryWhere.And(m => m.CategoryId == queryParam["categoryId"].ToString());
                }
                //关键字（名称、编码）
                if (!string.IsNullOrEmpty(input.keyword))
                {
                    queryWhere = queryWhere.And(m => m.FullName.Contains(input.keyword) || m.EnCode.Contains(input.keyword));
                }
                return await db.GetIQueryable<DataInterfaceEntity>().Where(queryWhere).OrderBy(t => t.SortCode).GetPageResultAsync(input);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<DataInterfaceEntity>> GetList()
        {
            using (var db = this.Basic)
            {
                return (await db.GetListAsync<DataInterfaceEntity>()).OrderBy(x => x.SortCode).ToList();
            }
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<DataInterfaceEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<DataInterfaceEntity>(m => m.Id == id);
            }
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<int> Create(DataInterfaceEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<int> Delete(DataInterfaceEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<int> Update(DataInterfaceEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }


    }
}
